<?php
namespace Home\Controller;
use Common\Lib\User;
use Home\Model\DepartmentModel;
use Home\Model\RoleModel;

class BasicGoodsController extends CommonController {
	protected $table="BasicGoods";
	protected $pageSize = 13;


	public function index (){
		$D = D('RbacUser');
		//获取所有CRM部门 (一维)
        $getalldepartmentsList_url = "Department/getAllDepartmentsList";
	    $alldepartmentsList = json_decode(http_get($getalldepartmentsList_url),true);
		//获取所有CRM部门 (二维)
        $getalldepartments_url = "Department/getAllDepartments";
	    $alldepartments = json_decode(http_get($getalldepartments_url),true);
		$ename = $this->getRoleEname();
    	$this->assign('viewDecorator', D("Department")->decoratorView($ename));
    	//部门选项权限
    	$ename=$this->getRoleEname();
    	//所属部门
    	$departmentInfo = D("Department")->getDepartment(session('account')['userInfo']['department_id']);
        $arr=$departmentInfo["list"];
        if($ename==RoleModel::GOLD||$ename==RoleModel::HR_MASTER || session('account')['userInfo']['department_id']==22){
        	 $departments=array('list'=>$alldepartments["list"],'account'=>'','group'=>'');    
        }else{
        	    //根据部门id获取所有组 (二维)
        	    $department_id=session('account')['userInfo']['department_id'];
        	    $field = "id,name";
			    $getgroupById_url = "Group/getGroupById?id=".$department_id."&field=".$field;
			    $crm_groups = json_decode(http_get($getgroupById_url),true);
                $departments=array('list'=>$arr,'account'=>$arr,'group'=>$crm_groups);
        }
        $kindsList = $this->M->where(array('pid'=>0))->getField('id,name', true);
        $kindsType = $this->M->where(array('pid'=>0))->field("id,name")->select();
    


     
     
        $this->assign('departments',$departments);
    	//获取crm的部门，职能 start
    	$this->assign('departmentsList',$alldepartmentsList["list"]);//一维
    	$this->assign('departmentsType',$alldepartments["list"]);//二维
    	//获取crm的部门，职能 end
    /*	$this->assign("kindsList",   $this->M->getKindsList(null));
		$this->assign("kindsType",   $this->M->getKindsType());*/
		$this->assign("kindsList",   $kindsList);
		$this->assign("kindsType",   $kindsType);
		$this->assign("propertyList",$this->M->getPropertyList(null));
		$this->assign("propertyType",$this->M->getPropertyType());
		
		$this->display();
	}

	public function setQeuryCondition() {
			$department_id = I('get.department_id');
			$user_id = session('account')['userInfo']['user_id'];
			switch(session('account')['userInfo']['role_id']){
				case 18:
				if(!empty($department_id)){
					$this->M->where(array('department_id'=>$department_id))->order("id desc");
				}else{
					$this->M->order("id desc");
				}
			    break;
			    default :
			    $this->M->where(array('department_id'=>session('account')['userInfo']['department_id'],'user_id'=>$user_id))
			        ->order("id desc");
			}
			$this->M->where(array('pid'=>array('NEQ', 0)));
			if (isset($_GET['name'])) {
			$this->M->where(array('name'=>array('like', I('get.name')."%")));
		    }
		    if (isset($_GET['kinds'])) {
		    		$this->M->where(array('kinds'=>$_GET['kinds']));
            }
	}
	/**
	* 添加
	*/
	public function add(){
	   
	    $_POST['department_id'] = session('account')['userInfo']['department_id'];
	    $_POST['user_id'] = session('account')['userInfo']['user_id'];
	    $arr = M("basicGoods")->field("path")->where(array("id"=>$_POST["pid"]))->select();
	   	$_POST["path"] = $arr["0"]["path"].$_POST["pid"].",";
	    $_POST["type"] = 2;
		$re = $this->M->create($_POST, 1);
		if ($re) {
			$this->M->startTrans(); 
			$re['recruitInfo'] = M('recruitInfo')->create($_POST, 1);
			$id = $this->M->add($re);
			if ($id) {
					$this->M->commit();
					$this->success(L('ADD_SUCCESS'));
			} else {
				$this->M->rollback();
				$this->error($this->M->getError());
			}
		} else {
			$this->error($this->M->getError());
		}

	}

	/**
	* 编辑
	*/
		public function edit(){
		/*	 echo 'ss';
		print_r($_POST);
		echo 'kk';
		exit();*/
        $re = $this->M->create($_POST, 2);
		if ($re) {
			if ($this->M->where(array('id'=>I('post.id') ))->save() !== false) {
				$this->success(L('ADD_SUCCESS'));
			} else {
				$this->error($this->M->getError());
			}
		} else {
			$this->error($this->M->getError());
		}

	}




}
